package jmind.core.jdbc;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.jdbc.core.RowMapper;
/**
 * 这种方式相对于 Tjdbc maven 可以编译
 * @author weibo-xie
 * 2011-12-20
 * @param <E>
 */
public abstract class EJdbc<E> extends BaseJdbc {
	
	

	public abstract E parse(ResultSet rs) throws SQLException;

	public E findOne(int source, String sql, Object... args) {
		ResultSetExtractor<E> res = new ResultSetExtractor<E>() {
			@Override
			public E extractData(ResultSet rs) throws SQLException, DataAccessException {
				if (rs.next()) {
					return parse(rs);
				}
				return null;
			}
		};

		return getJdbc(source).query(sql, res);
	}

	public List<E> find(int source, String sql, Object... args) {
		List<E> list = getJdbc(source).query(sql, new RowMapper<E>() {
			public E mapRow(ResultSet rs, int rowNumber) throws SQLException {
				return parse(rs);
			}
		},args);
		return list;
	}
}
